package com.lyy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyy.Vo.ProductCategoryTreeNodeVO;
import com.lyy.Vo.UserKeyWord;
import com.lyy.converter.CategoryTreeBuilder;
import com.lyy.converter.ProductCategoryConverter;
import com.lyy.handler.BusinessException;
import com.lyy.response.ResultCode;
import com.lyy.system.entity.Product;
import com.lyy.system.entity.ProductCategory;
import com.lyy.system.mapper.ProductCategoryMapper;
import com.lyy.system.mapper.ProductMapper;
import com.lyy.system.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<ProductCategoryTreeNodeVO> findProductCategoryList(UserKeyWord userKeyWord) {
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
        if (userKeyWord !=null){
            wrapper.like("name",userKeyWord.getKeyWord());
        }
        List<ProductCategory> productCategoryList=this.baseMapper.findProductCategoryList(wrapper);
        List<ProductCategoryTreeNodeVO> nodeVOS= ProductCategoryConverter.converterToTreeNodeVO(productCategoryList);
        return CategoryTreeBuilder.build(nodeVOS);
    }

    @Override
    public void addProductCategory(ProductCategory productCategory) {
        Long parentId=productCategory.getPid();
        QueryWrapper<ProductCategory> wrapper =new QueryWrapper<>();
        wrapper.eq("pid",parentId).orderByDesc("create_time").last(" limit 1");
        ProductCategory productCategory1=this.baseMapper.selectOne(wrapper);
        if (productCategory1 == null){
            productCategory.setSort(1);
        }else {
            productCategory.setSort(productCategory1.getSort()+1);
        }
        this.baseMapper.insert(productCategory);
    }

    @Override
    public void deleteProductCategory(Long id) {
        ProductCategory category = this.baseMapper.selectById(id);
        if (category == null){
            throw new BusinessException(ResultCode.PRODUCTCATEGORY_NOT_EXIST.getCode(),
                    ResultCode.PRODUCTCATEGORY_NOT_EXIST.getMessage());
        }else {
            //检查是否存在子类
            QueryWrapper<ProductCategory> wrapper =new QueryWrapper<>();
            wrapper.eq("pid",id);
            Integer count =this.baseMapper.selectCount(wrapper);
            if (count !=0){
                throw new BusinessException(ResultCode.PRODUCTCATEGORY_ERROR.getCode(),
                        ResultCode.PRODUCTCATEGORY_ERROR.getMessage());
            }

            //检查分类是否有物资引用
            QueryWrapper<Product> wrapper1 =new QueryWrapper<>();
            wrapper1.eq("one_category_id",id).or().eq("two_category_id",id).or().eq("three_category_id",id).eq("status",0);
            Integer count1=productMapper.selectCount(wrapper1);
            if (count1 !=0){
                throw new BusinessException(ResultCode.PRODUCTCATEGORY_PRODUCT.getCode(),
                        ResultCode.PRODUCTCATEGORY_PRODUCT.getMessage());
            }
            this.baseMapper.deleteById(id);
        }
    }

    @Override
    public void updateProductCategory(ProductCategory productCategory) {
        this.baseMapper.updateById(productCategory);
    }

    @Override
    public ProductCategory productCategoryDetail(Long id) {
        ProductCategory productCategory=this.baseMapper.selectById(id);
        return productCategory;
    }
}
